﻿using System;
using System.Drawing;
using System.Windows.Forms;
using Framework.UI.Template.Common;
using Framework.Core.Exception;

namespace Framework.UI.Template.CommonForm
{
    public partial class ExceptionForm : DevExpress.XtraEditors.XtraForm
    {
        private readonly string _hideDetailText = "隐藏明细";

        private readonly Exception _innerException;

        private readonly string _showDetailText = "显示明细";

        public ExceptionForm()
        {
            InitializeComponent();
        }

        public ExceptionForm(Exception exception)
        {
            InitializeComponent();

            if (exception != null)
            {
                _innerException = exception;
                string message = _innerException.ToString();
                message = message + "\n" + _innerException.StackTrace;
                memException.Text = message;

                ExceptionHelper.HandleException(exception, true, false);
            }

            Size = new Size(420, 150);
        }

        public virtual void RefreshFrameworkLanguage()
        {
            if (TemplateCommon.LanguageHelper != null)
            {
                Text = "系统异常";
                //lblException.Text = "应用程序异常";
                //lblSendToAdmin.Text = "发送给管理员";
                btnShowDetail.Text = "隐藏明细";
                btnClose.Text = "关闭";
            }
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            if (pnlExceptionInformation.Visible)
            {
                pnlExceptionInformation.Visible = false;
                btnShowDetail.Text = _showDetailText;
                Size = new Size(420, 150);
            }
            else
            {
                pnlExceptionInformation.Visible = true;
                btnShowDetail.Text = _hideDetailText;
                Size = new Size(420, 260);
            }
        }

        private void ShowExceptionForm_Load(object sender, EventArgs e)
        {
            RefreshFrameworkLanguage();

            pnlExceptionInformation.Visible = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}